/*
 * @Author: xcl
 * @Date: 2022-10-12 09:05:11
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-12 10:30:54
 * @Description: [国际化工具]
 */

import translations from './translations'

export default function customTranslate(template, replacements) {
  replacements = replacements || {}

  // 翻译
  template = translations[template] || template

  // 替换
  return template.replace(/{([^}]+)}/g, function (_, key) {
    var str = replacements[key]
    if (
      translations[replacements[key]] != null &&
      translations[replacements[key]] !== 'undefined'
    ) {
      str = translations[replacements[key]]
    }
    return str || '{' + key + '}'
  })
}

// export default function customTranslate(template, replacements) {
//   replacements = replacements || {}

//   // 翻译
//   template = translations[template] || template

//   // 替换
//   return template.replace(/{([^}]+)}/g, function (_, key) {
//     return replacements[key] || '{' + key + '}'
//   })
// }